package demo_table.frame;

import demo_table.dialog.SelectStudentDialog;
import demo_table.dialog.UpdateStudentDialog;
import demo_table.util.DataUtil;
import demo_table.dialog.AddStudentDialog;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DownPane extends JPanel {

    JButton b1, b2, b3, b4;

    TableFrame tableFrame;

    public DownPane(TableFrame tableFrame){
        this.tableFrame = tableFrame;
        //this.setLayout(null);
        b1 = new JButton("添加学生");
        b2 = new JButton("删除学生");
        b3 = new JButton("修改学生信息");
        b4 = new JButton("查看详细信息");

        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new AddStudentDialog(tableFrame);
            }
        });

        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int id = tableFrame.getSdp().getLovoTable().getKey();
                if(id == -1){
                    JOptionPane.showMessageDialog(null, "请点击你需要删除的学生");
                }else{
                    DataUtil.deleteStudent(id);
                    JOptionPane.showMessageDialog(null, "删除成功");
                    tableFrame.getSdp().getLovoTable().updateLovoTable(DataUtil.getAllStduent());
                }
            }
        });

        b3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new UpdateStudentDialog(tableFrame);
            }
        });

        b4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new SelectStudentDialog(tableFrame);
            }
        });

        this.add(b1);
        this.add(b2);
        this.add(b3);
        this.add(b4);
    }
}
